<?php

namespace App\Http\Controllers;

use App\Models\Permission;
use Illuminate\Http\Request;

class PermissionsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $permissionstree = Permission::getPermissionTree(Permission::orderBy('sort')->get(), 0, 0, 3);

        return view('admin.permissions.index', compact('permissionstree'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $routename = $this->routeNameList();
        $permissionstree = Permission::getPermissionTree(Permission::orderBy('sort')->get());

        return view('admin.permissions.create', compact('permissionstree', 'routename'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'pid' => 'required|numeric',
            'display_name' => 'required|unique:permissions',
            'name' => 'nullable|max:50',
            'icon' => 'nullable|string',
            'description' => 'nullable|max:191',
            'is_show' => 'required|boolean',
            'sort'=> 'required|numeric|max:100|min:0'
        ]);

        $Permission = new Permission;

        $Permission->pid = $request->pid;
        $request->filled('name') && $Permission->name = $request->name;
        $Permission->display_name = $request->display_name;
        $request->filled('icon') && $Permission->icon = $request->icon;
        $Permission->description = $request->description;
        $Permission->is_show = $request->is_show;
        $Permission->sort = $request->sort;

        if ($Permission->save()) {
            session()->flash('success', '保存成功');
            return redirect()->route('permissions.index');
        } else {
            session()->flash('danger', '保存失败');
            return redirect()->back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $permissions = Permission::find($id);
        $permissionstree = Permission::getPermissionTree(Permission::orderBy('sort')->get());
        $routename = $this->routeNameList();

        return view('admin.permissions.edit', compact('permissions', 'permissionstree', 'routename'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'pid' => 'required|numeric',
            'display_name' => 'nullable|unique:permissions',
            'name' => 'nullable|max:50',
            'icon' => 'nullable|string',
            'description' => 'nullable|max:191',
            'is_show' => 'required|boolean',
            'sort'=> 'required|numeric|max:100|min:0'
        ]);

        $Permission = Permission::find($id);

        $Permission->pid = $request->pid;
        $request->filled('name') && $Permission->name = $request->name;
        $request->filled('display_name') && $Permission->display_name = $request->display_name;
        $request->filled('icon') && $Permission->icon = $request->icon;
        $Permission->description = $request->description;
        $Permission->is_show = $request->is_show;
        $Permission->sort = $request->sort;

        if ($Permission->save()) {
            session()->flash('success', '保存成功');
            return redirect()->route('permissions.index');
        } else {
            session()->flash('danger', '保存失败');
            return redirect()->back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $permission = Permission::find($id);
        if ($permission->delete()) {
            session()->flash('success', '删除成功');
        } else {
            session()->flash('danger', '删除失败');
        }
        return back();
    }

    /**
     * 批量添加菜单路由节点
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function batchcreate()
    {
        $permissionstree = Permission::getPermissionTree(Permission::orderBy('sort')->get());
        $routename = $this->routeNameList();

        return view('admin.permissions.batchcreate', compact('permissionstree', 'routename'));
    }

    /**
     * @param Request $request
     */
    public function batchstore(Request $request)
    {
        $arr['display_name'] = $request->display_name;
        $arr['name'] = $request->name;
        $arr['is_show'] = $request->is_show;

        for ($i = 0; $i < count($arr['is_show']); $i++) {
            $data[$i]['pid'] = $request->pid;
            $data[$i]['display_name'] = $arr['display_name'][$i];
            $data[$i]['name'] = $arr['name'][$i];
            $data[$i]['is_show'] = $arr['is_show'][$i];
        }

        $Permission = new Permission;
        if ($Permission->insert($data)) {
            session()->flash('success', '保存成功');
            return redirect()->route('permissions.index');
        } else {
            session()->flash('danger', '保存失败');
            return redirect()->back();
        }
    }

    private function routeNameList()
    {
        // 遍历路由列表
        foreach (\Route::getRoutes() as $val) {
            // 判断该路由是否需要登录认证
            if (in_array('auth', $val->action['middleware'])) {
                $routename[] = $val->action['as'];
            }
        }
        return $routename;
    }
}
